package com.swq.java.code.chapter11.code09;

public class Name {
    private final String firstname;
    private final String lastname;

    public Name(String firstname, String lastname) {
        this.firstname = firstname;
        this.lastname = lastname;
    }

    public String getFirstname() {
        return firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Name)) return false;

        final Name name = (Name) o;
        if (!firstname.equals(name.firstname)) return false;
        if (!lastname.equals(name.lastname)) return false;
        return true;
    }

    public int hashCode() {
        int result;
        result = (firstname == null ? 0 : firstname.hashCode());
        result = 29 * result + (lastname == null ? 0 : lastname.hashCode());
        return result;
    }

    public String toString() {
        return lastname + " " + firstname;
    }

    public static void main(String args[]) {
        Name n1 = new Name("小红", "王");
        Name n2 = new Name("小红", "王");
        Name n3 = new Name("小东", "张");
        System.out.println(n1);
        System.out.println(n2);
        System.out.println(n3);
        System.out.println(n1 == n2);
    }
}
